<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2020-08-01
 * Time: 下午 08:38
 */
namespace Action;

use Action\Common\BaseLayout;
use Action\Page\View;
use Lib\Action;
use Lib\Functions;

/**
 * Class Index
 * @package Action
 * 
 * @property-read \Lib\Config $config
 * @property-read \Model\Page $pageModel
 */
class Index extends BaseLayout {
    public $data;
    public $reservedPath = ['static/', 'shared/'];
    public $templates = null;
    public $pageList = null;
    

    /**
     * action 入口文件
     */
    public function main() {
        $url = $this->fns->newUrlCreator();
        $path = $url->getPath();
        
        if(preg_match('#^/page/([^/\\\\]+)$#i', $path, $match)) {
            /** @var View $view */
            $view = di()->get(View::class);
            $view->main($match[1]);
            die;
        } else if($path != '/') {
            $this->notfound();
            die;
        }
        
        if(!$_SESSION['logined']) {
            $this->core->redirectToAction('login');
        }
        
        $this->templateData['header']['title'] = '后台管理系统';
        $this->view();
    }


    /**
     * 获取下次排序内容
     * @param $name
     * @return string
     */
    public function getNextSort($name) {
        $sort = explode('-', $_GET['sort'], 2);

        if($name == $sort[0]) {
            return [
                'asc' => 'asc',
                'desc' => 'desc',
            ][$sort[1]] ?: 'none';
        }

        return 'none';
    }

    /**
     * 获取页面配置参数
     * @return array
     */
    public function getPageList() {
        if(!$this->pageList) {
            $this->pageList = $this->pageModel->getPages();
        }

        return $this->pageList;
    }
    
    /**
     * 创建面包屑数组
     * @param $requestPath
     * @return \string[][]
     */
    public function createBreadcrumbs($requestPath) {
        $breadcrumbs = [[
            'name' => '根目录',
            'url' => '/'
        ]];
        
        $dir = '';
        
        foreach(array_filter(explode('/', $requestPath)) as $item) {
            $relative = "$dir$item";
            
            if($this->isReservedName($relative)) {
                $url = '/?path=' . $relative;
            } else {
                $url = '/' . $relative;
            }
            
            $breadcrumbs []= [
                'name' => $item,
                'url' => $url
            ];

            $dir .= "$item/";
        }
        
        $count = count($breadcrumbs);
        $breadcrumbs[$count-1]['url'] = null;
        return $breadcrumbs;
    }

    

    /**
     * 获取排序url
     * 根据当前字段的排序内容,计算下一次点击之后的排序url
     * @param $name
     * @return mixed|string
     */
    public function getSortUrl($name) {
        $sort = explode('-', $_GET['sort'], 2);
        $direct = $sort[1];
        $direct = $direct ?: '';
        
        if($name == $sort[0]) {
            $vals = ['asc'=> 0, 'desc'=> 1, '' => 2];
            $valKeys = array_keys($vals);
            $val = $vals[$direct];
            
            $newDirect = $valKeys[($val+1)%count($valKeys)];
            return $this->fns->buildCurrentUrl(['sort' => $newDirect?"{$sort[0]}-{$newDirect}":null]);
        }

        return $this->fns->buildCurrentUrl(['sort' => "$name-asc"]);
    }
    
    public function humamSize($size) {
        $type = ['Bytes','KB','MB','GB','TB'];
        
        for($i = 0; $size >= 1024; $i++) {
            if($i == count($type)) {
                break;
            }
            
            $size/=1024;
        }
        
        return (floor($size*100)/100).' ' . $type[$i];
    }
}
